package org.crmsystem.service.impl;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.crmsystem.dao.DeptMapper;
import org.crmsystem.dao.StaffMapper;
import org.crmsystem.model.Dept;
import org.crmsystem.model.Staff;
import org.crmsystem.service.IDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 部门服务层实现
 * @author 孙艳平
 */
@Service
public class DeptServiceImpl implements IDeptService{
    @Autowired
    private DeptMapper deptMapper;
    @Autowired
    private StaffMapper staffMapper;
    Logger logger=LogManager.getLogger(DeptServiceImpl.class);
    /**
     * 根据id获取部门详细信息
     */
    public Dept getDeptById(int id) {
        return deptMapper.getDeptById(id);
    }

    /**
     * 多条件查询获取部门列表
     */
    public List<Dept> getDept(Dept dept) {
        return deptMapper.getDept(dept);
    }

    /**
     * 添加部门
     */
    public int addDept(Dept dept) {
        return deptMapper.addDept(dept);
    }
    /**
     * 删除部门
     */
    @Transactional
    public int delDept(int id) {
    	Staff staff=new Staff();
    	staff.setDeptId(id);
    	Map map=new HashMap();
    	map.put("staff", staff);
    	map.put("currPage", 0);
    	map.put("pageSize", 0);
    	List<Object> list=staffMapper.getStaff(map);
    	for(int i=0;i<list.size();i++){
    		Staff staff1=(Staff)list.get(i);
    		staff1.setDeptId(-1);
    		int flag=staffMapper.updateStaff(staff1);
    	}
        return deptMapper.delDept(id);
    }
    /**
     * 更新部门
     */
    public int updateDept(Dept dept) {
        return deptMapper.updateDept(dept);
    }
}